Back to course home
0% completed
Vote For New Content
LinkedIn Hard
Max Points on a Line (hard)
Solution: Max Points on a Line
Closest Binary Search Tree Value II (hard)
Solution: Closest Binary Search Tree Value II
Word Ladder (hard)
Solution: Word Ladder
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Find Leaves of Binary Tree (medium)
Problem Statement
Given the root of a binary tree, collect a tree's leaf nodes by following the given rules and return them in a 2D array.
Collectall the leaf nodes from theleft to right.Removeall the leaf nodes.Repeatuntil the tree is empty.
Examples
- Example 1:
- Input: root =
[1,2,3,4]
- Input: root =
-
Expected Output:
[[4,3],[2],[1]] -
Justification: Initially, nodes 4 and 3 are leaves and are removed in the first round. In the second round, node 2 becomes a leaf. Node 1 is the last one remaining and is removed in the final round.
-
Example 2:
- Input: root =
[1,2,3,null, null,4,5,null, null,6,null]
- Input: root =
-
Expected Output:
[[2,4,6],[5],[3],[1]] -
Justification: In the first round, the leaves are nodes 2, 4, and 6. Once removed, node 5 becomes the leaf in the second round, and then node
3becomes the leaf node in the third round. Finally, node 1 is left and removed in the last round. -
Example 3:
- Input: root =
[1,2,null,3,null,4]
- Input: root =
- Expected Output:
[[4],[3],[2],[1]] - Justification: This tree has a linear structure. Each node becomes a leaf one after the other, starting from node 4 to node 1.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Rotate List
Solution: Find Leaves of Binary Tree
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself